<?php

namespace app\api\controller;

use addons\epay\library\Service;
use app\admin\model\goods\Order;
use app\common\controller\Api;
use app\common\controller\Payment;
use think\Controller;
use think\Db;
use think\Request;


class VideoLists extends Api
{
    private static $model;
    protected $noNeedLogin = ['lists'];
    protected $noNeedRight = '*';


    public function __construct(Request $request = null)
    {
        parent::__construct($request);
        self::$model = new \app\common\model\VideoLists();
    }

    /**
     * 显示资源列表
     *
     * @return \think\Response
     */
    public function lists()
    {
        $pageSize = input('pagesize', 15);

        $where = [
            'status' => 1
        ];

        $isRecom = input('is_recom');
        if ($isRecom) {
            $where['is_recom'] = 1;
        }

        $cateId = input('cate_id');
        if ($cateId) {
            $where['cate_id'] = $cateId;
        }

        $order = 'weigh desc';
        $isNew = input('is_new');
        if ($isNew) {
            $order = "createtime desc";
        }

        $list = self::$model->where($where)->order($order)->paginate($pageSize);

        $this->success('教学视频列表', $list);
    }

    /**
     * 视频详情
     * @param int $cateId
     * @param int $id
     */
    public function read($cateId = 0, $id = 0)
    {
        $userId = $this->auth->id;
        $where = [
            'status' => 1
        ];

        if ($cateId) {
            $where['cate_id'] = $cateId;
        }

        if ($id) {
            $where['id'] = $id;
        }

        $row_info = self::$model->where($where)->order('weigh desc')->find();
        if (!$row_info) {
            $this->error('该视频不存在！');
        }

        $praiseId = \app\common\model\VideoPraises::where('video_id', $row_info->id)->where('user_id', $userId)->where('comment_id', 0)->value('id');
        $collectId = \app\common\model\VideoCollects::where('video_id', $row_info->id)->where('user_id', $userId)->value('id');
        $row_info->is_praise = $praiseId ? 1 : 0;
        $row_info->is_collect = $collectId ? 1 : 0;

        $this->success('视频详情', $row_info);
    }
}
